package leetcode.easy;

/**
 * Created by sheny on 2017/5/19.
 */
public class ReverseInteger_7 {
    public static void main(String[] args){
        System.out.println(reverse(-2147483648));
    }
    public static int reverse(int x) {
        if(x == -2147483648){
            return 0;
        }
        char[] c = String.valueOf(Math.abs(x)).toCharArray();
        StringBuilder sb = new StringBuilder();
        for(int i = c.length-1;i >= 0;i--){
            sb.append(c[i]);
        }
        long result = 0;
        if(x < 0){
            result = Long.parseLong("-"+sb.toString());
        }else{
            result = Long.parseLong(sb.toString());
        }
        if(result < -2147483648 || result > 2147483647 ){
            return 0;
        }
        return (int)result;
    }

    public static int reverse2(int x){
        int result = 0;
        for(int i = 32;i>0;i--){
            result += ((x/10^i)*(x/32^i));
        }
        return result;
    }
}
